package myself.study.network.tcp.examples.ex02;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;

/**
* @Name: TcpClient
* @Description: 案例二：客户端键盘录入请求数据
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 下午8:58:05 
* @Version: V1.0
 */
public class TcpClient {
	
	@Test
	public void client() throws UnknownHostException, IOException {
		//创建客户端Socket对象
		Socket client = new Socket("127.0.0.1", 12345) ;
		//获取键盘输入的数据，并放入到高效缓冲输入流中
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) ;
		//获取客户端输出通道，并包装成高效缓冲输出流
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())) ;
		String line = null ;
		while((line = reader.readLine()) != null) {
			//设置键盘终止标志
			if("over".equals(line)) {
				break ;
			}
			//将键盘输入的数据，作为客户端请求数据发送到服务端
			writer.write(line) ;
			//键入换行符
			writer.newLine() ;
			//刷新缓冲区
			writer.flush() ;
		}
		//释放资源
		client.close() ;
	}

}







